package com.caishi.lkx.user.api;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.caishi.lkx.common.AdminBaseApi;
import com.caishi.lkx.common.role.Roles;
import com.caishi.lkx.user.ienum.type.HotQuestionType;
import com.caishi.lkx.user.model.HotQuestionModel;
import com.caishi.lkx.user.service.IHotQuestionService;
import com.zzw.common.Wrappers;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.List;

@Tag(name = "常见问题相关接口")
@RestController
@RequestMapping("/user/sys/hot-question")
public class HotQuestionApi extends AdminBaseApi<IHotQuestionService, HotQuestionModel, String> {

    @Resource
    private IHotQuestionService hotQuestionService;

    @Override
    public IHotQuestionService loadService() {
        return hotQuestionService;
    }

    @Override
    protected LambdaQueryWrapper<HotQuestionModel> baseWrapper(HotQuestionModel model, JSONObject otherParam) {
        String name = model.getName();
        model.setName(null);
        LambdaQueryWrapper<HotQuestionModel> hotWordModelLambdaQueryWrapper = super.baseWrapper(model, otherParam);
        return hotWordModelLambdaQueryWrapper.like(StrUtil.isNotBlank(name),HotQuestionModel::getName,name);
    }
    @GetMapping("/page")
    @Roles
    public IPage<HotQuestionModel> page(@ModelAttribute Page<HotQuestionModel> page, @ModelAttribute HotQuestionModel model, @NotNull JSONObject otherParam) {
        IPage<HotQuestionModel> data = loadService().page(loadService().pageProcessing(page), baseWrapper(model, otherParam));
        data.setRecords(listCover(data.getRecords(), otherParam));
        return data;
    }

    @Override
    protected List<HotQuestionModel> listCover(List<HotQuestionModel> result, JSONObject otherParam) {
        return result;
//        return result.parallelStream().peek(d -> {
//                d.setCreatedByName(userService.queryName(d.getCreatedBy()));
//        }).collect(Collectors.toList());
    }

    @GetMapping("/app-list")
    @Operation(summary = "常见问题列表")
    @Parameters({
            @Parameter(name = "size",description = "搜索长度"),
            @Parameter(name = "name",description = "关键词"),
            @Parameter(name = "type",description = "热词类型"),
    })
    @Roles
    public List<HotQuestionModel> list(@RequestParam(defaultValue = "10") Integer size, @RequestParam(required = false) String name, @RequestParam(defaultValue = "course") HotQuestionType type) {
        if (size > 1000) {
            size = 1000;
        }
        return loadService().selectList(Wrappers.<HotQuestionModel>lambdaQuery().eq(HotQuestionModel::getType,type).like(StrUtil.isNotBlank(name),HotQuestionModel::getName,name).orderByAsc(HotQuestionModel::getSort).last("limit "+size));
    }

    @DeleteMapping("/deleted-list")
    public Boolean delete(@RequestParam List<String> ids) {
        return loadService().delete(ids);
    }

}
